package com.jzkj.zsqxkq.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jzkj.zsqxkq.entity.TblMoniSiteInfo;
import com.jzkj.zsqxkq.service.ITblMoniSiteInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 周毅
 * @since 2020-07-06
 */

@Controller
@RequestMapping("/info")
public class TblMoniSiteInfoController {

    @Resource
    private ITblMoniSiteInfoService tblMoniSiteInfoService;


    @PostMapping(value = "/save")
    @ResponseBody
    public int saveInfo(TblMoniSiteInfo siteInfo) {
        Assert.notNull(siteInfo, "add moinSiteInfo is null");
        Assert.notNull(siteInfo.getSitecode(), "add moinSiteInfo siteCode is null");
        Assert.notNull(siteInfo.getSitename(), "add moinSiteInfo siteName is null");
        Assert.notNull(siteInfo.getLongitude(), "add moinSiteInfo longitude is null");
        Assert.notNull(siteInfo.getLatitude(), "add moinSiteInfo latitude is null");


        LambdaQueryWrapper<TblMoniSiteInfo> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(TblMoniSiteInfo::getSitename, siteInfo.getSitename());
        TblMoniSiteInfo serviceOne = tblMoniSiteInfoService.getOne(lambdaQuery);

        Assert.isTrue(null == serviceOne, "sitename existed");

        siteInfo.setSiteid(UUID.randomUUID().toString());
        siteInfo.setCreatetime(LocalDateTime.now());
        siteInfo.setLastupdatetime(LocalDateTime.now());
        boolean save = tblMoniSiteInfoService.save(siteInfo);
        return save ? 1 : -1;
    }

    @PutMapping(value = "/modify")
    @ResponseBody
    public int modifyInfo(TblMoniSiteInfo siteInfo) {
        Assert.notNull(siteInfo, "modify moinSiteInfo is null");
        Assert.notNull(siteInfo.getSitecode(), "modify moinSiteInfo siteCode is null");
        Assert.notNull(siteInfo.getSitename(), "modify moinSiteInfo siteName is null");
        Assert.notNull(siteInfo.getLongitude(), "modify moinSiteInfo longitude is null");
        Assert.notNull(siteInfo.getLatitude(), "modify moinSiteInfo latitude is null");
        boolean updateById = tblMoniSiteInfoService.updateById(siteInfo);
        siteInfo.setLastupdatetime(LocalDateTime.now());
        return updateById ? 1 : -1;
    }

    @ResponseBody
    @DeleteMapping(value = "/del/{delId}")
    public int delInfoById(@PathVariable("delId") String delId) {
        Assert.notNull(delId, "del json is null");
        boolean remove = tblMoniSiteInfoService.removeById(delId);
        return remove ? 1 : -1;
    }

    @ResponseBody
    @PostMapping(value = "/del")
    public int delInfoByJson(String delJson) {
        Assert.notNull(delJson, "del json is null");
        List<String> delJsonList = JSON.parseArray(delJson, String.class);
        boolean remove = tblMoniSiteInfoService.removeByIds(delJsonList);
        return remove ? 1 : -1;
    }

    @GetMapping(value = "/page")
    public String pageInfo(Model model, Integer pageNum, Integer pageSize) {
        if (null == pageNum || pageNum <= 0) {
            pageNum = 1;
        }
        if (null == pageSize || pageSize <= 0) {
            pageSize = 10;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<TblMoniSiteInfo> list = tblMoniSiteInfoService.list();
        PageInfo<TblMoniSiteInfo> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("pageNum", pageNum);
        return "info-list";
    }


    @GetMapping(value = "/jump/{id}")
    public ModelAndView jumpModifyInfo(@PathVariable("id") String id, ModelAndView modelAndView) {
        Assert.notNull(id, "id is null");
        TblMoniSiteInfo byId = tblMoniSiteInfoService.getById(id);
        modelAndView.addObject("info", byId);
        modelAndView.setViewName("info-modify");
        return modelAndView;
    }

    @ResponseBody
    @GetMapping(value = "/hasName/{infoName}")
    public int hasInfoName(@PathVariable("infoName") String infoName) {
        LambdaQueryWrapper<TblMoniSiteInfo> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(TblMoniSiteInfo::getSitename, infoName);
        return tblMoniSiteInfoService.count(lambdaQuery);
    }

}
